//
//  LHRecommendHomePageScrollView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-21.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendHomePageScrollView.h"

@interface LHRecommendHomePageScrollView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *homePageScrollView;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSTimer *timeTour;
@property (nonatomic, assign) NSInteger i_count;

@end

@implementation LHRecommendHomePageScrollView


- (void)dealloc
{
    [_homePageScrollView release];
    [_page release];
    [_timeTour release];
    
    [super dealloc];
}

// 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _i_count = self.frame.size.width;
        
        [self createHomePageScrollView];
        [self createHomePage];
    }
    
    return self;
}

// 创建推荐游首页轮播图
- (void)createHomePageScrollView
{

    
    self.homePageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    _homePageScrollView.backgroundColor = [UIColor colorWithRed:0.120 green:0.745 blue:0.576 alpha:1.000];
    
    _homePageScrollView.pagingEnabled = YES;
    
    _homePageScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    
    _homePageScrollView.contentSize = CGSizeMake(self.frame.size.width * 6, 0);
    
    _homePageScrollView.showsHorizontalScrollIndicator = NO;
    
    _homePageScrollView.showsVerticalScrollIndicator = NO;
    
    _homePageScrollView.delegate = self;
    
    [self addSubview:_homePageScrollView];
    
    [_homePageScrollView release];
    
    
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 40, _homePageScrollView.frame.size.height - 30, 80, 30)];
    
    _page.numberOfPages = 4;
    
    _page.currentPage = 0;
    
    _page.pageIndicatorTintColor = [UIColor colorWithRed:0.470 green:0.190 blue:0.073 alpha:1.000];
    
    _page.currentPageIndicatorTintColor = [UIColor colorWithRed:0.303 green:0.905 blue:0.603 alpha:1.000];
    
    [_page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:_page];
    
    [_page release];
    
    
    // 创建第一张和最后一张照片
    UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    scrollView1.backgroundColor = [UIColor colorWithRed:0.160 green:0.527 blue:0.073 alpha:1.000];
    
    scrollView1.contentSize = CGSizeMake(self.frame.size.width, 0);
    
    scrollView1.delegate = self;
    
    [_homePageScrollView addSubview:scrollView1];
    
    [scrollView1 release];
    
    UIImageView *imgae1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    imgae1.image = [UIImage imageNamed:@"followpk4.jpg"];
    
    [scrollView1 addSubview:imgae1];
    
    [imgae1 release];
    
    UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * 5, 0,self.frame.size.width, self.frame.size.height)];
    
    scrollView2.backgroundColor = [UIColor colorWithRed:0.160 green:0.527 blue:0.073 alpha:1.000];
    
    scrollView2.delegate = self;
    
    scrollView2.contentSize = CGSizeMake(self.frame.size.width, 0);
    
    [_homePageScrollView addSubview:scrollView2];
    
    [scrollView2 release];
    
    UIImageView *imgae2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    imgae2.image = [UIImage imageNamed:@"followpk1.jpg"];
    
    [scrollView2 addSubview:imgae2];
    
    [imgae2 release];

    //添加定时器
    self.timeTour = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeOver) userInfo:nil repeats:YES];
    
    
}

// 定时器方法
- (void)timeOver
{
    
    [_homePageScrollView setContentOffset:CGPointMake(self.frame.size.width + _i_count, 0)];
//    _homePageScrollView.contentOffset = CGPointMake(_homePageScrollView.contentOffset.x + self.frame.size.width, 0);
    
    _page.currentPage = _homePageScrollView.contentOffset.x / self.frame.size.width - 1;
    
    _i_count += self.frame.size.width;
    
    if (_homePageScrollView.contentOffset.x > self.frame.size.width * 4) {
        
        _homePageScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        _i_count = self.frame.size.width;
        
        _page.currentPage = _homePageScrollView.contentOffset.x / self.frame.size.width - 1 ;
    }
    
}


// 创建相册
- (void)createHomePage
{
    NSInteger x = self.frame.size.width;
    
    for (NSInteger i = 1; i < 5; i++) {
        
        UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(x, 0, self.frame.size.width, self.frame.size.height)];
        
        scrollview.backgroundColor = [UIColor colorWithRed: arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.000];
        
        scrollview.contentSize = CGSizeMake(LHWidth, 0);
        
        scrollview.minimumZoomScale = 0.5;
        
        scrollview.maximumZoomScale = 2;
        
        scrollview.delegate = self;
        
        [_homePageScrollView addSubview:scrollview];
        
        [scrollview release];
        
        NSString *name = [NSString stringWithFormat:@"followpk%ld.jpg", i];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        imageView.image = [UIImage imageNamed:name];
        
        [scrollview addSubview:imageView];
        
        [imageView release];
        
        x = x + self.frame.size.width;
        
    }

}

- (void)page:(UIPageControl *)page
{
    _homePageScrollView.contentOffset = CGPointMake(_page.currentPage * self.frame.size.width + self.frame.size.width, 0);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    _page.currentPage = _homePageScrollView.contentOffset.x / self.frame.size.width - 1;
    
  
    
    if (_homePageScrollView.contentOffset.x < self.frame.size.width) {
        
        _homePageScrollView.contentOffset = CGPointMake(self.frame.size.width * 4, 0);
        
        _page.currentPage = _homePageScrollView.contentOffset.x / self.frame.size.width ;
        
    }
    if (_homePageScrollView.contentOffset.x > self.frame.size.width * 4) {
        
        _homePageScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        
        _page.currentPage = _homePageScrollView.contentOffset.x / self.frame.size.width - 1 ;
    }
    
    

    
}






@end
